package src

import (
    "log"

    dns "github.com/alibabacloud-go/alidns-20150109/v4/client"
    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    util "github.com/alibabacloud-go/tea-utils/v2/service"
    "github.com/alibabacloud-go/tea/tea"
)

type AliyunService struct {
    client *dns.Client
}

func NewAliyunService(accessKeyId string, accessKeySecret string) *AliyunService {

    config := &openapi.Config{
        // 必填，您的 AccessKey ID
        AccessKeyId: &accessKeyId,
        // 必填，您的 AccessKey Secret
        AccessKeySecret: &accessKeySecret,
    }
    // 访问的域名
    config.Endpoint = tea.String("alidns.cn-hangzhou.aliyuncs.com")
    client, err := dns.NewClient(config)
    if err != nil {
        log.Fatalln(err)
    }
    return &AliyunService{client}
}

func (s *AliyunService) DescribeDomainRecords(domainName string, RR string, recordType string) (*dns.DescribeDomainRecordsResponse, error) {

    req := &dns.DescribeDomainRecordsRequest{}
    // 主域名
    req.DomainName = &domainName
    // 主机记录
    req.RRKeyWord = &RR
    // 解析记录类型
    req.Type = &recordType
    resp, tryErr := func() (_r *dns.DescribeDomainRecordsResponse, _e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        resp, _err := s.client.DescribeDomainRecords(req)
        if _err != nil {
            return nil, _err
        }

        return resp, _err
    }()

    if tryErr != nil {
        var error = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            error = _t
        } else {
            error.Message = tea.String(tryErr.Error())
        }
        log.Println(error.Message)
    }
    return resp, nil
}

func (s *AliyunService) UpdateDomainRecord(recordId string, ip string, RR string, recordType string) (_err error) {

    // 修改解析记录
    req := &dns.UpdateDomainRecordRequest{}
    // 主机记录
    req.RR = &RR
    // 记录ID
    req.RecordId = &recordId
    // 将主机记录值改为当前主机IP
    req.Value = &ip
    // 解析记录类型
    req.Type = &recordType

    tryErr := func() (_e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        _, _err := s.client.UpdateDomainRecord(req)
        if _err != nil {
            return _err
        }
        return nil
    }()

    if tryErr != nil {
        var error = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            error = _t
        } else {
            error.Message = tea.String(tryErr.Error())
        }
        log.Println(error.Message)
    }
    return _err
}

func (s *AliyunService) CreateRecord(ip string, rr string, domain string) error {

    addDomainRecordRequest := &dns.AddDomainRecordRequest{}
    addDomainRecordRequest.Value = &ip
    addDomainRecordRequest.RR = &rr
    addDomainRecordRequest.DomainName = &domain
    addDomainRecordRequest.Type = tea.String("A")

    runtime := &util.RuntimeOptions{}
    tryErr := func() (_e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        // 复制代码运行请自行打印 API 的返回值
        _, _err := s.client.AddDomainRecordWithOptions(addDomainRecordRequest, runtime)
        if _err != nil {
            return _err
        }

        return nil
    }()

    if tryErr != nil {
        var error = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            error = _t
        } else {
            error.Message = tea.String(tryErr.Error())
        }
        // 如有需要，请打印 error
        _, _err := util.AssertAsString(error.Message)
        if _err != nil {
            return _err
        }
    }
    return nil
}
